define(function(require, exports, module) {
    'use strict';
    
    var rk = require('rk');
    require('./waveGuiderCtrl');

    var animateDuration = 300;

    $.widget('rk.userGuiderCtrl', {
        _create: function() {
            var me = this;
            var elem = me.element;
            me.showMode();
            rk.oa.closePanel();
            rk.oa.closeContent();

            var edition = 'standard';
            if(window.SESSION.tenantType == '1'){
                edition = 'industryFMCG'
            }

            require.async('page/tmpl/frame/userGuider/'+edition+'/loader', function(oTmpl) {
                me.oTmpl = oTmpl;
                me.initStep0();
            });
        },
        showMode: function() {
            $('#container').find('.main-content').append('<div id="guider-shade" class="oa-shade-div" style="display: block;cursor: auto;"></div>');
            $(document.body).append('<div id="guider-shade2" style="position: fixed; opacity: .2; background: #000;z-index:102;width: 100%;height: 55px;left:0;top:0;"></div>');
        },
        deleteMode: function() {
            $('#guider-shade').remove();
            $('#guider-shade2').remove();
        },
        initStep0: function() {
            var me = this;
            window.guider_opening = true; //避免引用纠缠
            var guider_left_menu = me.oTmpl.guider_left_menu;
            var guider_step0 = me.oTmpl.step0_popup;
            $(document.body).append(guider_left_menu);
            $(document.body).append(guider_step0);
            //$('#fake_pageleft_unfold').css('z-index', 100000);
            $('#privatemsg').hide();
            var myid = 'guider_step0';
            var popup = $('#' + myid);
            popup.show();
            popup.find('a.close').click(function() {
                popup.off().remove();
                $('#guider_left_menu').remove();
                me._clean();
            });
            popup.find('a[act="nextStep"]').click(function() {
                popup.off().remove();
                me.initStep1();
            });
        },
        initStep1: function() {
            var me = this;
            var html = me.oTmpl.step1;
            $(document.body).append(html);

            var id = 'guider_step1';
            var popup = $('#' + id);
            popup.fadeIn()
            popup.find('a[act="nextStep"]').click(function() {
                popup.off().remove();
                me.initStep2();
            });
        },
        initStep2: function() {
            var me = this;
            var html = me.oTmpl.step2_popup;
            $(document.body).append(html);
            var html = me.oTmpl.step2_oapanel;
            $('#pagecontent').find('.oa-panel').show().append(html);
            var html = me.oTmpl.step2_oamenu_btn;
            $('#pagehead').append(html);
            var html = me.oTmpl.step2_oamenu;
            $('#pagehead').append(html);

            _showOAPanel(function() {
                $('#oamenubar').pageOAMenuCtrl('showOpeningIcon');
                _showOAMenu(function() {
                    _showOAPopup();
                });
            });

            function _showOAPanel(succ) {
                $('#guider_step2_oa_panel').show().animate({
                    right: '0px'
                }, {
                    duration: animateDuration,
                    complete: function() {
                        (succ)();
                    }
                });
            }

            function _showOAMenu(succ) {
                $('#guider_step2_oa_menu').show().animate({
                    top: '0px'
                }, {
                    duration: animateDuration,
                    complete: function() {
                        (succ)();
                    }
                });
            }

            function _showOAPopup() {
                var id = 'guider_step2_popup';
                var popup = $('#' + id);
                popup.fadeIn();
                popup.find('a[act="nextStep"]').click(function() {
                    popup.off().remove();
                    _hideOAMenu(function() {
                        _hideOAPanel(function() {
                            $('#oamenubar').pageOAMenuCtrl('showClosingIcon');
                            $('#guider_step2_oa_panel').remove();
                            $('#guider_step2_oa_menu').parent().remove();
                            $('#guider_step2_oa_menu_btn').remove();
                            me.initStep3();
                        });

                    });

                });
            }

            function _hideOAMenu(succ) {
                $('#guider_step2_oa_menu').animate({
                    top: (-300) + 'px'
                }, {
                    duration: animateDuration,
                    complete: function() {
                        (succ)();
                    }
                });
            }

            function _hideOAPanel(succ) {
                $('#guider_step2_oa_panel').animate({
                    right: (-1 * $('#guider_step2_oa_panel').width() + 20) + 'px'
                }, {
                    duration: animateDuration,
                    complete: function() {
                        $('#pagecontent').find('.oa-panel').hide();
                        (succ)();
                    }
                });
            }
        },
        initStep3: function() {
            var me = this;
            var elem = me.element;
            var html = me.oTmpl.step3_messager;
            $('#container').append(html);
            var html = me.oTmpl.step3_popup;
            $('#container').append(html);

            function _showMsg(succ) {
                $('#guider_step3_messager').show().animate({
                    bottom: '0px'
                }, {
                    duration: animateDuration,
                    complete: function() {
                        (succ)();
                    }
                });
            }

            function _hideMsg(succ) {
                $('#guider_step3_messager').animate({
                    bottom: '-500px'
                }, {
                    duration: animateDuration,
                    complete: function() {
                        (succ)();
                    }
                });
            }

            _showMsg(function() {
                $('#guider_left_menu').fadeIn();
            });


            var id = 'guider_step3_popup';
            var popup = $('#' + id);
            popup.fadeIn()
            popup.find('a[act="nextStep"]').click(function() {
                popup.off().remove();
                _hideMsg(function() {
                    $('#guider_step3_messager').fadeOut().remove();
                    $('#guider_left_menu').fadeOut().remove();
                    me._clean();
                });
            });

        },
        _clean: function() {
            var me = this;
            me.deleteMode();
            delete window.guider_opening;
            //$(document.body).find('img[id^="wave_"]').off().remove();
            $(window).userGuiderCtrl('destroy');
            if ($(window).data('waveinited')) $(window).waveGuiderCtrl('destroy');
            $(window).waveGuiderCtrl();
            $(window).data('waveinited', true);
            $('#privatemsg').show();
            me._report();
        },
        _report: function() {
            var me = this;
            var opt = me.options;
            rk.post("/json/global_menu/guide.action", {
                userGuide: 0
            }).done(function(data) {});
            if(opt.done) (opt.done)();
        }
    });
});
